home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / phigs / ptk.lha / ptk / source / demo / cnstest.c next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  4.8 KB  |  202 lines

  1. /*--------------------------------------------------------------------------- 
  2.  
  3.  Program name: Colour naming scheme.
  4.  
  5.  Author: Gareth Williams
  6.  
  7.  Description: Demonstrates the Colour Naming Scheme of the PHIGS Toolkit.
  8.  Users may type in colour names and the colour will be displayed on the
  9.  screen.
  10.  
  11.  Modification history : (Version), (Date), (Name), (Description).
  12.  
  13.  1.0, 18th February 1991, G. Williams, First Version.
  14.  
  15.  2.0, June 1992, G. Williams, Converted to ISO PHIGS C.
  16.  
  17.  SunOS requirements: SunPHIGS 2.0, OpenWindows 3.0.
  18.  
  19. ----------------------------------------------------------------------------*/
  20.  
  21. #include <stdio.h>
  22. #include <math.h>
  23. #include <phigs.h>
  24. #include "ptk.h"
  25.  
  26. #define WS1     1
  27. #define TABLE   1
  28.  
  29. /* Operating system dependent code, UNIX/VMS pathnames */
  30. /* UNIX pathname */
  31. #define OPENWSNAME "../scripts/openws.scr"
  32.  
  33. /* for gamma correction */
  34. #define GAMMA 1.0
  35. #ifdef SUN
  36. #undef GAMMA
  37. #define GAMMA 1.94
  38. #endif
  39. #ifdef PEXSI
  40. #undef GAMMA
  41. #define GAMMA 1.94
  42. #endif
  43. #ifdef HP
  44. #undef GAMMA
  45. #define GAMMA 2.56
  46. #endif
  47.  
  48. static Pfloat devx, devy, devz;
  49.  
  50. /*--------------------------------------------------------------------------*/
  51.  
  52. static void outputcolourvalues(C(Pint) inum, C(char *) colourname,
  53.                                C(Prgb *) rgb)
  54. PreANSI(Pint inum)
  55. PreANSI(char *colourname)
  56. PreANSI(Prgb *rgb)
  57. /* Output colour model values of colourname in RGB, HSV and HSL
  58. ** coordinates.
  59. */
  60. {
  61.   Phls hsl;
  62.   Phsv hsv;
  63.  
  64.   printf("%d) RGB value of %s is %f %f %f\n", inum, colourname, rgb->red, 
  65.            rgb->green, rgb->blue); 
  66.   ptk_rgbtohsv(rgb, &hsv);
  67.   printf("%d) HSV value of %s is %f %f %f\n", inum, colourname, hsv.hue, 
  68.            hsv.satur, hsv.value); 
  69.   ptk_rgbtohsl(rgb, &hsl);
  70.   printf("%d) HSL value of %s is %f %f %f\n\n", inum, colourname, hsl.hue, 
  71.            hsl.satur, hsl.lightness); 
  72. }
  73.  
  74. /*--------------------------------------------------------------------------*/
  75.  
  76. static void options(C(void))
  77. /* enable input of colour names using PHIGS string device */
  78. {
  79.   char colourname[50];
  80.   Pint err, lencolourname;
  81.   ptkboolean cnsquit;
  82.   Plimit echoarea;
  83.   Pcolr_rep rgb;
  84.  
  85.   cnsquit = FALSE;
  86.   echoarea = ptk_limit(0.0, devx, 0.0, devy * 0.1);
  87.   do
  88.   {
  89.     /* input a colour name and display the colour */
  90.     ptk_readstring(WS1, "white", "Input colourname (white) >", &echoarea,
  91.                    50, colourname, &lencolourname);
  92.     if (strncmp(colourname, "quit", lencolourname) == 0)
  93.     {
  94.       cnsquit = TRUE;
  95.     }    
  96.     else
  97.     {
  98.       /* convert colourname to RGB colour model */
  99.       ptk_cnstorgb(colourname, &rgb.rgb);
  100.  
  101.       /* gamma correct */
  102.       rgb.rgb.red = pow(rgb.rgb.red, 1.0/GAMMA);
  103.       rgb.rgb.green = pow(rgb.rgb.green, 1.0/GAMMA);
  104.       rgb.rgb.blue = pow(rgb.rgb.blue, 1.0/GAMMA);
  105.  
  106.       /* set entry in colour table */
  107.       pset_colr_rep(WS1, 1, &rgb);
  108.       pempty_struct(TABLE);
  109.       /* display entry in colour table */
  110.       ptk_drawcolourtable(TABLE, 1, 1);      
  111.       outputcolourvalues(1, colourname, &rgb.rgb);
  112.     }
  113.     pupd_ws(WS1, PFLAG_PERFORM);
  114.   } while (cnsquit == FALSE);
  115. }
  116.  
  117. /*--------------------------------------------------------------------------*/
  118.  
  119. main()
  120. {
  121.   FILE *fileptr;
  122.   Pint i, numcolours;
  123.   char colourname[64], dummystr[255];
  124.   Pint dummylen;
  125.   Plimit echoarea;
  126.   Pint wst;
  127.  
  128.   /* open PHIGS */
  129.  
  130.   printf("Demonstrating the Colour Naming Scheme of the PHIGS Toolkit...\n");
  131.  
  132.   /* Implementation dependent code, open PHIGS and workstation */
  133. #ifdef SUN
  134.   printf("Opening SunPHIGS...\n");
  135.   popen_phigs(PDEF_ERR_FILE, PDEF_MEM_SIZE);
  136.  
  137.   /* open the workstation */
  138.   ptk_readphinterscript(OPENWSNAME, NULL, NULL);    
  139.     
  140. /* create the workstation type (tool) 
  141.  
  142.   wst = phigs_ws_type_create( phigs_ws_type_x_tool,
  143.         PHIGS_COLOUR_MODE, PHIGS_INDEX_COLOUR,
  144.     PHIGS_TOOL_LABEL, "SunPHIGS Tool Workstation",
  145.     0);
  146.   if ( !wst ) 
  147.   {
  148.     pclose_phigs();
  149.     exit(1);
  150.   }
  151.  
  152.   popen_ws(WS1, (void *)NULL, wst);
  153.   {
  154.     Pws_st    ws_state;
  155.     
  156.     pinq_ws_st(&ws_state);
  157.     if (ws_state != PWS_ST_WSOP)
  158.       exit(3);
  159.   }
  160. */
  161. #endif
  162. #ifdef PEXSI
  163.   printf("Opening PEX-SI PHIGS...\n");
  164.   popen_phigs(PDEF_ERR_FILE, PDEF_MEM_SIZE);
  165.  
  166.   /* open the workstation */
  167.   ptk_readphinterscript(OPENWSNAME, NULL, NULL);    
  168. #endif
  169. #ifdef HP
  170.   printf("Opening HP PHIGS...\n");
  171.   popen_phigs(stderr, PDEF_MEM_SIZE);
  172.  
  173.   /* open the workstation */
  174.   ptk_readphinterscript(OPENWSNAME, NULL, NULL);    
  175. #endif
  176.  
  177.   ptk_inqmaxdevicecoords(WS1, &devx, &devy);
  178.   devz = 0.0;
  179.  
  180.   /* set up hashstrings colour table */
  181.   ptk_inithashtables();
  182.   ptk_createhashtable("colourindex", 1, 256);
  183.  
  184.   pset_disp_upd_st(WS1, PDEFER_WAIT, PMODE_NIVE);
  185.  
  186.   ptk_drawcolourtable(TABLE, 0, 0);  
  187.   ppost_struct(WS1, TABLE, 0.0);
  188.  
  189.   pupd_ws(WS1, PFLAG_PERFORM);
  190.  
  191.   options();
  192.  
  193.   pclose_ws(WS1);
  194.   printf("Closing PHIGS...\n");
  195.   pclose_phigs();
  196.   exit(0);
  197. }
  198.  
  199. /*--------------------------------------------------------------------------*/
  200.  
  201. /* end of cnstest.c */
  202.